package com.xiangxiao.rpan.authority.service.impl;

import com.xiangxiao.rpan.authority.factory.ValidateCodeFactory;
import com.xiangxiao.rpan.authority.service.ImgVerifyCodeService;
import com.xiangxiao.rpan.authority.vo.LoginVerifyImgVo;
import com.xiangxiao.rpan.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/7/4 15:45
 */
@Service
public class ImgVerifyCodeServiceImpl implements ImgVerifyCodeService {
  @Value("${verify.code.length}")
  private int verifyCodeLength;
  @Override
  public LoginVerifyImgVo generatorCaptcha() {
    int w = 200, h = 80;
    String verifyCode = ValidateCodeFactory.generateVerifyCode(this.verifyCodeLength);
    String verifyCodeImage = null;

    try {
      verifyCodeImage = ValidateCodeFactory.outputImage(200, 80, verifyCode);
    } catch (Exception e) {
      verifyCodeImage = "生成验证码失败";
    };

    LoginVerifyImgVo vo = new LoginVerifyImgVo();
    vo.setImgBase64(verifyCodeImage);
    vo.setUuid(UUIDUtil.getUUID());

    return vo;
  }
}
